package group.rober.dataform.handler.impl;

import com.google.common.collect.Lists;
import group.rober.dataform.handler.DataListHandler;
import group.rober.dataform.model.DataForm;
import group.rober.dataform.model.DataFormElement;
import group.rober.saber.jdbc.accessor.DataAccessor;
import group.rober.saber.jdbc.accessor.listener.InsertListener;
import group.rober.saber.jdbc.accessor.listener.UpdateListener;
import group.rober.saber.jdbc.query.PaginationQueryResult;
import group.rober.saber.jdbc.query.executor.PaginationQueryExecutor;
import group.rober.saber.kit.DataBoxKit;
import group.rober.saber.kit.ValidateKit;
import group.rober.saber.lang.DataBox;
import group.rober.saber.lang.ValueObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 默认实现DataBox列表数据的基本处理
 * Created by tisir<yangsong158@qq.com> on 2017-05-29
 */
@Component("dataBoxListHandler")
public class DataBoxListHandler implements DataListHandler<DataBox> {
    @Autowired
    protected PaginationQueryExecutor queryExecutor;
    @Autowired
    private DataAccessor accessor;


    public PaginationQueryExecutor getQueryExecutor() {
        return queryExecutor;
    }

    public void setQueryExecutor(PaginationQueryExecutor queryExecutor) {
        this.queryExecutor = queryExecutor;
    }

    public DataAccessor getAccessor() {
        return accessor;
    }

    public void setAccessor(DataAccessor accessor) {
        this.accessor = accessor;
    }

    public void beforeQuery(DataForm dataForm, Map<String, Object> queryParameters, Map<String, ValueObject> filterParameters,Map<String,String> sortMap, int pageSize, int pageIndex) {

    }

    public PaginationQueryResult<DataBox> query(DataForm dataForm, Map<String, Object> queryParameters, Map<String, ValueObject> filterParameters, Map<String,String> sortMap, int pageSize, int pageIndex) {
        this.beforeQuery(dataForm,queryParameters,filterParameters,sortMap,pageSize,pageIndex);

        FunctionInHandler.ListQuery handlerListQuery = new FunctionInHandler.ListQuery<DataBox>(queryExecutor,dataForm);
        handlerListQuery.setQueryParameters(queryParameters);
        handlerListQuery.setFilterParameters(filterParameters);
        handlerListQuery.setSortParameters(sortMap);
        handlerListQuery.setPageSize(pageSize);
        handlerListQuery.setPageIndex(pageIndex);
        PaginationQueryResult<DataBox> result = handlerListQuery.queryPaginationList(DataBox.class);

        this.afterQuery(dataForm,queryParameters,filterParameters,sortMap,pageSize,pageIndex,result);

        return result;
    }

    public void afterQuery(DataForm dataForm, Map<String, Object> queryParameters, Map<String, ValueObject> filterParameters,Map<String,String> sortMap, int pageSize, int pageIndex,PaginationQueryResult<DataBox> result) {

    }

    public List<DataBox> save(final DataForm dataForm, final List<DataBox> dataList) {
        ValidateKit.notNull(dataForm,"dataform cannot be empty while saving a databox list");
        List<DataFormElement> pkElementList = dataForm.getPrimaryKeyElements();
        ValidateKit.notEmpty(pkElementList,"dataform does not have a primary key set ,while saving a databox,dataform=%s",dataForm.getFullName());

        String table = dataForm.getTable();
        List<Map<String,Object>> entityList = Lists.newArrayList();
        List<Map<String,Object>> keyAttributeList = Lists.newArrayList();

        //处理列映射转换
        for(DataBox dataItem : dataList){
            entityList.add(dataItem);
        }
        //处理主键部分
        for(DataFormElement element : pkElementList){

        }

        ValidateKit.notEmpty(keyAttributeList,"dataform does not have a primary key data set ,while saving a databox,dataform=%s",dataForm.getFullName());



        //保存数据以及保存后的消息回调钩子
        accessor.saveMap(table, entityList, keyAttributeList, new InsertListener<List<Map<String, Object>>>() {
            public void before(List<Map<String, Object>> data) {
                beforeInsert(dataForm, DataBoxKit.convertToDataBoxList(data));
            }
            public void after(List<Map<String, Object>> data) {
                afterInsert(dataForm,DataBoxKit.convertToDataBoxList(data));
            }
        }, new UpdateListener<List<Map<String, Object>>>() {
            public void before(List<Map<String, Object>> data) {
                beforeUpdate(dataForm,DataBoxKit.convertToDataBoxList(data));
            }
            public void after(List<Map<String, Object>> data) {
                afterUpdate(dataForm,DataBoxKit.convertToDataBoxList(data));
            }
        });
        return dataList;
    }

    public void beforeInsert(DataForm dataForm, List<DataBox> dataList) {

    }

    public void afterInsert(DataForm dataForm, List<DataBox> dataList) {

    }

    public void beforeUpdate(DataForm dataForm, List<DataBox> dataList) {

    }

    public void afterUpdate(DataForm dataForm, List<DataBox> dataList) {

    }

    public Integer delete(DataForm dataForm, List<DataBox> dataList) {
        return null;
    }

    public void beforeDelete(DataForm dataForm, List<DataBox> dataList) {

    }

    public void afterDelete(DataForm dataForm, List<DataBox> dataList) {

    }
}
